package com.jrsoft.bim.db;

import com.jrsoft.bim.dto.*;
import com.jrsoft.bim.dto.excel.ComponentDTO;
import com.jrsoft.bim.dto.excel.FieldDTO;
import com.jrsoft.bim.enums.ComponentCategoryEnum;

import java.util.Map;
import java.util.Vector;

/**
 * ${DESCRIPTION}
 *
 * @author Blueeyedboy
 * @create 2019-12-31 5:49 PM
 **/
public class DBV2Util {

	public ConnDB db;

	public DBV2Util(ConnDB db){
		this.db = db;
	}

	public long getCount(String sql){
		return db.getCount(sql);
	}
	public Vector<Map<String,Object>> getData(String sql){
		return db.getData(sql);
	}

	public void insertWall(ComponentDTO entity) {
		insertComponent(entity);
	}
	public void insertBridge(ComponentDTO entity){
		insertComponent(entity);

	}

	public void insertComponent(ComponentDTO entity){
		String mainTable = "";
		String subTable = "";
		if(entity.getCategoryEnum().equals(ComponentCategoryEnum.BRIDGE)){
			mainTable = "T_BRIDGE";
			subTable = "T_BRIDGE_COLUMN";
		}else if(entity.getCategoryEnum().equals(ComponentCategoryEnum.WALL)){
			mainTable = "T_WALL";
			subTable = "T_WALL_COLUMN";
		}
		StringBuilder sql1 = new StringBuilder();
		sql1.append("insert into "+mainTable+"(NO_,CODE_,LEVEL_,V1_,V2_,V3_,V4_,V5_,V6_,V7_,V71_,V8_,V9_,V10_,V11_,V12_)values(")
				.append(entity.getId())
				.append(",").append("'"+entity.getCode()+"'")
				.append(",").append(entity.getLevel())
				.append(",").append(entity.getV1())
				.append(",").append(entity.getV2())
				.append(",").append(entity.getV3())
				.append(",").append(entity.getV4())
				.append(",").append(entity.getV5())
				.append(",").append(entity.getV6())
				.append(",").append(entity.getV7())
				.append(",").append(entity.getV71())
				.append(",").append(entity.getV8())
				.append(",").append(entity.getV9())
				.append(",").append(entity.getV10())
				.append(",").append(entity.getV11())
				.append(",").append(entity.getV12())
				.append(");");
		db.executeUpdateSql(sql1.toString());

		// 循环插入字段属性
		if(entity.getFieldDTOMap()!= null){
			int index = 0;
			for(Map.Entry<String,FieldDTO> field : entity.getFieldDTOMap().entrySet()){
				FieldDTO f = field.getValue();
				index++;
				f.setId(index);
				f.setCmpId(entity.getId());
				insertField(f,subTable);
			}
		}

	}

	public void insertField(FieldDTO entity,String subTable){
		StringBuilder sql1 = new StringBuilder();
		sql1.append("insert into "+subTable+"(NO_,CMP_ID_,CODE_,UNIT_,VALUE_)values(")
				.append(entity.getId())
				.append(",").append(entity.getCmpId())
				.append(",").append("'"+entity.getCode()+"'")
				.append(",").append("'"+entity.getUnit()+"'")
				.append(",").append("'"+entity.getValue()+"'")
				.append(");");
		db.executeUpdateSql(sql1.toString());

	}


	public void insertTblFloor(FloorDTO entity){


		StringBuilder sql2 = new StringBuilder();
		sql2.append("insert into tblFloor(ID,No_,name,StdFlrID,LevelB,Height)values(")
				.append(entity.getId())
				.append(",").append(entity.getNo())
				.append(",").append("''")
				.append(",").append(entity.getStdFlrDTO().getId())
				.append(",").append(entity.getLevelB())
				.append(",").append(entity.getHeight())
				.append(");");
		db.executeUpdateSql(sql2.toString());
	}

	public void insertTblGrid(GridDTO entity){
		StringBuilder insertSql = new StringBuilder();
		insertSql.append("insert into tblGrid(ID,No_,StdFlrID,Jt1ID,Jt2ID,AxisID)values(")
				.append(entity.getId())
				.append(",").append(entity.getNo())
				.append(",").append(entity.getStdFlrDTO().getId())
				.append(",").append(entity.getStart().getId())
				.append(",").append(entity.getEnd().getId())
				.append(",").append(entity.getAxis().getId())
				.append(");");
		db.executeUpdateSql(insertSql.toString());
	}

	public void insertTblBeamSeg(BeamSegDTO entity){
		StringBuilder insertSql = new StringBuilder();
		insertSql.append("insert into tblBeamSeg(ID,No_,StdFlrID,SectID,GridID,Ecc,HDiff1,HDiff2,Rotation,JYDef)values(")
				.append(entity.getId())
				.append(",").append(entity.getNo())
				.append(",").append(entity.getStdFlrDTO().getId())
				.append(",").append(entity.getBeamSect())
				.append(",").append(entity.getGrid().getId())
				.append(",").append(entity.getEcc())
				.append(",").append(0)
				.append(",").append(0)
				.append(",").append(0)
				.append(",").append(0)
//				.append(",").append("'0,0,0,0,'")
				.append(");");
		db.executeUpdateSql(insertSql.toString());
	}

	public void insertTblColSeg(ColSegDTO entity){
		StringBuilder insertSql = new StringBuilder();
		insertSql.append("insert into tblColSeg(ID,No_,StdFlrID,SectID,JtID,EccX,EccY,Rotation,HDiffB,ColcapId,Cut_Col,Cut_Cap,Cut_Slab)values(")
				.append(entity.getId())
				.append(",").append(entity.getNo())
				.append(",").append(entity.getStdFlrDTO().getId())
				.append(",").append(entity.getSectID())
				.append(",").append(entity.getJointDTO().getId())
				.append(",").append(entity.getEccX())
				.append(",").append(entity.getEccY())
				.append(",").append(0)
				.append(",").append(0)
				.append(",").append(0)
				.append(",").append("''")
				.append(",").append("''")
				.append(",").append("''")
				//				.append(",").append("'0,0,0,0,'")
				.append(");");
		db.executeUpdateSql(insertSql.toString());
	}

	public void insertTblJoint(JointDTO entity){
		StringBuilder insertSql = new StringBuilder();
		insertSql.append("insert into tblJoint(ID,No_,StdFlrID,X,Y,HDIFF)values(")
				.append(entity.getId())
				.append(",").append(entity.getNo())
				.append(",").append(entity.getStdFlrDTO().getId())
				.append(",").append(entity.getX())
				.append(",").append(entity.getY())
				.append(",").append(0)
				.append(");");
		db.executeUpdateSql(insertSql.toString());
	}

	public void insertTblAxis(AxisDTO entity){
		StringBuilder insertSql = new StringBuilder();
		insertSql.append("insert into tblAxis(ID,No_,StdFlrID,Jt1ID,Jt2ID,Name)values(")
				.append(entity.getId())
				.append(",").append(entity.getNo())
				.append(",").append(entity.getStdFlrDTO().getId())
				.append(",").append(entity.getStart().getId())
				.append(",").append(entity.getEnd().getId())
				.append(",").append("''")
				.append(");");
		db.executeUpdateSql(insertSql.toString());
	}
}
